/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javalee.dao;

//import com.mysql.jdbc.Statement;

import javalee.maps.Cliente;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;


/**
 *
 * @author Danilo
 */
public class clienteDAO extends DAO{
    private Statement stmt = null;

public boolean cadastrar(Object obj){
        
        try{
            Cliente c = (Cliente) obj;
            String SQL = "insert into cliente (nome) values ('"+c.getNome()+"')";
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
            // ex.printStackTrace();
             return (false);
       }
        
    }
public boolean alterar(Object obj){
         try{
            Cliente c = (Cliente) obj;
            String SQL = "update cliente set nome='"+c.getNome()+"' where id = "+c.getId();
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
           //  ex.printStackTrace();
             return (false);
       }
        }
public boolean excluir(Object obj){
    try{
            Cliente c = (Cliente) obj;
            String SQL = "delete from cliente where id = "+c.getId();
            stmt = conexaoBanco.createStatement();
            stmt.executeUpdate(SQL);
            stmt.close();
            //"insert into cliente (nome) values ('?')"
            return (true);
        } 
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
             System.out.println(ex.getErrorCode());
             
             if(ex.getErrorCode()==1451)
             {
                 JOptionPane.showMessageDialog(null,"Existe uma venda relacionada a este cliente.\nCliente não pode ser excluído.");
             }
            // ex.printStackTrace();
             return (false);
       }}
public ArrayList consultar(Object obj){
        
             ArrayList<Cliente> lista = new ArrayList<Cliente>();
        //List<Person> personlist = new List<Person>();
        try {
            stmt = conexaoBanco.createStatement();
            
            String query = "SELECT id, nome from cliente";
            if(!("".equals(obj.toString()))){
                query = query.concat(" WHERE nome LIKE '"+obj.toString()+"%'");
            }
            query+=" limit 100";
            ResultSet srs = stmt.executeQuery(query);
            while (srs.next()) {
                Cliente c = new Cliente();
                c.setNome(srs.getString("nome"));
                c.setId(srs.getInt("id"));
                
                lista.add(c);
            }
           
            stmt.close();
        }
        
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
             //ex.printStackTrace();
             return (null);
       }
 return lista;
}
public Cliente consultarID(int objeto){
        
          Cliente c = new Cliente();
          
        try {
            stmt = conexaoBanco.createStatement();
            
            
            ResultSet srs = stmt.executeQuery("SELECT id, nome from cliente WHERE id ="+objeto+" LIMIT 1");
            while (srs.next()) {
                
                c.setNome(srs.getString("nome"));
                c.setId(srs.getInt("id"));             
               
            }
           
            stmt.close();
        }
        
           catch (SQLException ex) {
             System.out.println("Problema com o SQL");
             //ex.printStackTrace();
             return (null);
       }
 return c;
}
}
